package com.clas.liu.myapplication.model.impl;

import com.clas.liu.myapplication.model.event.BaseEvent;
import com.clas.liu.myapplication.network.NetworkInterfaces;

/**
 * fragment的model层基类，通过接口实现与presenter的消息传递
 * Created by liu on 2019/2/28.
 */

public abstract class BaseFragmentModel<T extends BaseEvent> {

    NetworkInterfaces interfaces;
    private OnEventReceiveListener<T> listener;

    public  BaseFragmentModel(OnEventReceiveListener<T> listener){
        interfaces = getNetworkInterface();
        this.listener = listener;
    }

    protected abstract NetworkInterfaces getNetworkInterface();

    void postEvent(T event){
        listener.eventReceive(event,event.getWhat());
    }

    public  void onDestroy(){
        if(listener != null){
            listener = null;
        }
    }

    /**
     * 与presenter交互的接口
     * @param <Y>
     */
    public interface OnEventReceiveListener<Y extends BaseEvent> {
        public void eventReceive(Y event, int what);
    }
}
